package com.fourforfo.fourmall.ware.service.impl;

import com.fourforfo.common.constant.WareConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.ware.dao.PurchaseDetailDao;
import com.fourforfo.fourmall.ware.entity.PurchaseDetailEntity;
import com.fourforfo.fourmall.ware.service.PurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String wareId = (String) params.get("wareId");
        String status = (String) params.get("status");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(wrapper->{
                wrapper.eq("id",key)
                .or().eq("purchase_id",key)
                .or().eq("sku_id",key);
            });
        }
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 更新所有purchaseDetail的purchaseId和status
     * @param purchaseId
     * @param purchaseDetailIds
     */
    @Transactional
    @Override
    public void updatePurchaseIdAndStatus(Long purchaseId, List<Long> purchaseDetailIds) {
        baseMapper.updatePurchaseDetail(purchaseId,purchaseDetailIds, WareConstant.PurchaseDetailStatusEnum.ASSIGEND.getCode());
    }

    /**
     * 改变采购需求的状态
     * @param purchaseIds
     */
    @Override
    public void updateStatusByPurchaseId(List<Long> purchaseIds) {
        baseMapper.updateStatus(purchaseIds,WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
    }

}